<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="姓氏" prop="firstName">
      <el-input v-model="dataForm.firstName" placeholder="姓氏"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="lastName">
      <el-input v-model="dataForm.lastName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phoneNumber">
      <el-input v-model="dataForm.phoneNumber" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="所在区域" prop="area">
      <el-input v-model="dataForm.area" placeholder="所在区域"></el-input>
    </el-form-item>
    <el-form-item label="具体地址" prop="detailAddress">
      <el-input v-model="dataForm.detailAddress" placeholder="具体地址"></el-input>
    </el-form-item>
    <el-form-item label="是否为默认地址" prop="isDefault">
      <el-input v-model="dataForm.isDefault" placeholder="是否为默认地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          addressId: 0,
          userId: '',
          firstName: '',
          lastName: '',
          gender: '',
          phoneNumber: '',
          area: '',
          detailAddress: '',
          isDefault: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          firstName: [
            { required: true, message: '姓氏不能为空', trigger: 'blur' }
          ],
          lastName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '所在区域不能为空', trigger: 'blur' }
          ],
          detailAddress: [
            { required: true, message: '具体地址不能为空', trigger: 'blur' }
          ],
          isDefault: [
            { required: true, message: '是否为默认地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.addressId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.addressId) {
            this.$http({
              url: this.$http.adornUrl(`/user/address/info/${this.dataForm.addressId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.address.userId
                this.dataForm.firstName = data.address.firstName
                this.dataForm.lastName = data.address.lastName
                this.dataForm.gender = data.address.gender
                this.dataForm.phoneNumber = data.address.phoneNumber
                this.dataForm.area = data.address.area
                this.dataForm.detailAddress = data.address.detailAddress
                this.dataForm.isDefault = data.address.isDefault
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/address/${!this.dataForm.addressId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'addressId': this.dataForm.addressId || undefined,
                'userId': this.dataForm.userId,
                'firstName': this.dataForm.firstName,
                'lastName': this.dataForm.lastName,
                'gender': this.dataForm.gender,
                'phoneNumber': this.dataForm.phoneNumber,
                'area': this.dataForm.area,
                'detailAddress': this.dataForm.detailAddress,
                'isDefault': this.dataForm.isDefault
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
